package com.xcjw.com.Base;

import android.app.Activity;

import com.xcjw.com.MainActivity;

import java.util.Iterator;
import java.util.Stack;

/**
 * Created by Administrator on 2016/7/13.
 * 作者： 阳奇
 * 描述： 通过堆栈管理器，对Stack进的存储Activity进行操作（推入，推出，弹出）
 * 版本： 1.0
 */
public class StackManager {
    /**
     * Stack 中对应的Activity列表  （也可以写做 Stack<Activity>）
     */
    private static Stack mActivityStack;
    private static StackManager mInstance;

    private StackManager() {
    }

    public static StackManager getInstance() {
        if (mInstance == null) {
            mInstance = new StackManager();
        }
        mActivityStack = new Stack();
        return mInstance;
    }

    /**
     * 推出栈顶Activity
     */
    public void popActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            mActivityStack.remove(activity);
            activity = null;
        }
    }

    /*主要功能：为了实现从哪来到哪去，就是　一个页面多个入口时，从不同入口进入,从不同入口出来

    * @clazz 弹出指定的clazz所在栈顶部的中所有Activity
    *
    *
    * */
    public void exitActivity(Class clazz) {
        boolean isExit = popTopActivitys(clazz);
        if (isExit) {
            Activity activity = currentActivity();
            popActivity(activity);
        }
    }

    /**
     * 获得当前栈顶Activity
     */
    public Activity currentActivity() {
        if (mActivityStack == null || mActivityStack.size() == 0) {
            return null;
        }
        return (Activity) mActivityStack.lastElement();
    }

    /**
     * 将当前Activity推入栈中
     */
    public void pushActivity(Activity activity) {
        if (mActivityStack == null) {
            mActivityStack = new Stack();
        }
        if (!isExists(activity.getClass())){
            mActivityStack.add(activity);
        }


    }
    public boolean isExists(Class clazz){
        Iterator stack = mActivityStack.iterator();
        while (stack.hasNext()){
            Activity act = (Activity)stack.next();
            if(act.getClass().getSimpleName().equals(clazz)){
                    return true;
            }
        }
        return false;
    }
    /**
     * 弹出指定的clsss所在栈顶部的中所有Activity
     *
     * @clsss : 指定的类
     */
    public boolean popTopActivitys(Class clsss) {
        //当前activity是否存在
        boolean isExistence = false;
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(clsss)) {
                isExistence = true;
                break;
            }
            popActivity(activity);
        }
        return isExistence;
    }


    /**
     * 弹出栈中所有Activity
     */
    public void popAllActivitys() {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            popActivity(activity);

        }
    }  /**
     * 弹出到HomeActivity
     */
    public void popHomeActivity() {
        while (true) {
            Activity activity = currentActivity();
            if (activity.getClass().getName().equals("MainActivity")) {
                break;
            }
            popActivity(activity);

        }
    }
    public boolean popfirstActivity(Class clazz) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(MainActivity.class)) {
                break;
            }
            if (activity.getClass().equals(clazz)) {
                return true;
            }
            popActivity(activity);
        }
        return false;
    }
}
